package com.wufun.overseas.joystick.map;

import android.view.KeyEvent;

import java.util.HashMap;

/**
 * Created by xiet on 2015/6/10.
 */
public class KeyMap {

    //=====file section=====
    public static final int      MAX_FILE_NUM = 4;
    public static final String[] inifiles     = {"key_map.ini", "", "", ""};

    //=====keys section=====
    public static final int[] defJoystickKeys = {KeyEvent.KEYCODE_BUTTON_SELECT, KeyEvent.KEYCODE_BUTTON_START,
            KeyEvent.KEYCODE_BUTTON_X, KeyEvent.KEYCODE_BUTTON_A, KeyEvent.KEYCODE_BUTTON_Y, KeyEvent.KEYCODE_BUTTON_B,
            KeyEvent.KEYCODE_BUTTON_L1, KeyEvent.KEYCODE_BUTTON_L2, KeyEvent.KEYCODE_BUTTON_R1, KeyEvent.KEYCODE_BUTTON_R2,
            KeyEvent.KEYCODE_DPAD_LEFT, KeyEvent.KEYCODE_DPAD_UP, KeyEvent.KEYCODE_DPAD_RIGHT, KeyEvent.KEYCODE_DPAD_DOWN, KeyEvent.KEYCODE_BUTTON_THUMBL, KeyEvent.KEYCODE_BUTTON_THUMBR};
    public static final int[] defKeyboardKeys = {KeyEvent.KEYCODE_DEL, KeyEvent.KEYCODE_ENTER,
            KeyEvent.KEYCODE_1, KeyEvent.KEYCODE_2, KeyEvent.KEYCODE_4, KeyEvent.KEYCODE_3,
            KeyEvent.KEYCODE_5, KeyEvent.KEYCODE_7, KeyEvent.KEYCODE_6, KeyEvent.KEYCODE_8,
            KeyEvent.KEYCODE_A, KeyEvent.KEYCODE_W, KeyEvent.KEYCODE_D, KeyEvent.KEYCODE_S, KeyEvent.KEYCODE_9, KeyEvent.KEYCODE_0};

    public static final String   keySection = "KEY";
    public static final String[] keysKey    = {"KEY_SELECT", "KEY_START", "KEY_X", "KEY_A", "KEY_Y", "KEY_B", "KEY_L1", "KEY_L2", "KEY_R1", "KEY_R2", "KEY_DPAD_LEFT", "KEY_DPAD_UP", "KEY_DPAD_RIGHT", "KEY_DPAD_DOWN", "KEY_BUTTON_THUMBL", "KEY_BUTTON_THUMBR"};

    //=====fba key-value section=====
    public static final String   fbaKeyMapSection = "FBA_KEY_MAP";
    public static final String[] fbaKeysKey       = {KeyCodes.KEY_SELECT.value() + "", KeyCodes.KEY_START.value() + "", KeyCodes.KEY_A.value() + "", KeyCodes.KEY_B.value() + "", KeyCodes.KEY_C.value() + "", KeyCodes.KEY_D.value() + "", KeyCodes.KEY_E.value() + "", KeyCodes.KEY_F.value() + "", (KeyCodes.KEY_A.value() | KeyCodes.KEY_B.value()) + "", (KeyCodes.KEY_C.value() | KeyCodes.KEY_D.value()) + "", (KeyCodes.KEY_A.value() | KeyCodes.KEY_B.value() | KeyCodes.KEY_C.value()) + ""};
    public static final String[] fbaDefKeysValue  = {"KEY_SELECT", "KEY_START", "KEY_X", "KEY_A", "KEY_Y", "KEY_B", "KEY_L1", "KEY_L2", "KEY_R1", "KEY_R2", "KEY_BUTTON_THUMBR"};

    //=====fc key-value section=====ok
    public static final String   fcKeyMapSection = "FC_KEY_MAP";
    public static final String[] fcKeysKey       = {KeyCodes.KEY_SELECT.value() + "", KeyCodes.KEY_START.value() + "", KeyCodes.KEY_D.value() + "", KeyCodes.KEY_C.value() + "", KeyCodes.KEY_B.value() + "", KeyCodes.KEY_A.value() + "", (KeyCodes.KEY_A.value() | KeyCodes.KEY_B.value()) + ""};
    public static final String[] fcDefKeysValue  = {"KEY_SELECT", "KEY_START", "KEY_A", "KEY_B", "KEY_X", "KEY_Y", "KEY_R1"};

    //=====sfc key-value section=====
    public static final String   sfcKeyMapSection = "SFC_KEY_MAP";
    public static final String[] sfcKeysKey       = {KeyCodes.KEY_SELECT.value() + "", KeyCodes.KEY_START.value() + "", KeyCodes.KEY_C.value() + "", KeyCodes.KEY_A.value() + "", KeyCodes.KEY_D.value() + "", KeyCodes.KEY_B.value() + "", KeyCodes.KEY_L1.value() + "", KeyCodes.KEY_R1.value() + ""};
    public static final String[] sfcDefKeysValue  = {"KEY_SELECT", "KEY_START", "KEY_X", "KEY_A", "KEY_Y", "KEY_B", "KEY_L1", "KEY_R1"};

    //=====psp key-value section=====
    public static final String   pspKeyMapSection = "PSP_KEY_MAP";
    public static final String[] pspKeysKey       = {KeyCodes.KEY_SELECT.value() + "", KeyCodes.KEY_START.value() + "", KeyCodes.KEY_C.value() + "", KeyCodes.KEY_A.value() + "", KeyCodes.KEY_D.value() + "", KeyCodes.KEY_B.value() + "", KeyCodes.KEY_L1.value() + "", KeyCodes.KEY_R1.value() + ""};
    public static final String[] pspDefKeysValue  = {"KEY_SELECT", "KEY_START", "KEY_X", "KEY_A", "KEY_Y", "KEY_B", "KEY_L1", "KEY_R1"};

    //=====gba key-value section=====
    public static final String   gbaKeyMapSection = "GBA_KEY_MAP";
    public static final String[] gbaKeysKey       = {KeyCodes.KEY_SELECT.value() + "", KeyCodes.KEY_START.value() + "", KeyCodes.KEY_C.value() + "", KeyCodes.KEY_B.value() + "", KeyCodes.KEY_D.value() + "", KeyCodes.KEY_A.value() + "", KeyCodes.KEY_R1.value() + "", KeyCodes.KEY_L1.value() + ""};
    public static final String[] gbaDefKeysValue  = {"KEY_SELECT", "KEY_START", "KEY_B", "KEY_X", "KEY_Y", "KEY_A", "KEY_R1", "KEY_L1"};


    //=====md key-value section=====
    public static final String   mdKeyMapSection = "MD_KEY_MAP";
    public static final String[] mdKeysKey       = {KeyCodes.KEY_SELECT.value() + "", KeyCodes.KEY_START.value() + "", KeyCodes.KEY_D.value() + "", KeyCodes.KEY_B.value() + "", KeyCodes.KEY_E.value() + "", KeyCodes.KEY_A.value() + "", KeyCodes.KEY_C.value() + "", KeyCodes.KEY_F.value() + ""};
    public static final String[] mdDefKeysValue  = {"KEY_SELECT", "KEY_START", "KEY_X", "KEY_B", "KEY_Y", "KEY_A", "KEY_R1", "KEY_L1"};


    //=====ps key-value section=====
    public static final String   psKeyMapSection = "PS_KEY_MAP";
    public static final String[] psKeysKey       = {KeyCodes.KEY_SELECT.value() + "", KeyCodes.KEY_START.value() + "", KeyCodes.KEY_C.value() + "", KeyCodes.KEY_A.value() + "", KeyCodes.KEY_B.value() + "", KeyCodes.KEY_D.value() + "", KeyCodes.KEY_L1.value() + "", KeyCodes.KEY_R1.value() + "", KeyCodes.KEY_L2.value() + "", KeyCodes.KEY_R2.value() + ""};
    public static final String[] psDefKeysValue  = {"KEY_SELECT", "KEY_START", "KEY_X", "KEY_A", "KEY_Y", "KEY_B", "KEY_L1", "KEY_R1", "KEY_L2", "KEY_R2"};


    //=====wsc key-value section=====
    public static final String   wscKeyMapSection = "WSC_KEY_MAP";
    public static final String[] wscKeysKey       = {KeyCodes.KEY_SELECT.value() + "", KeyCodes.KEY_START.value() + "", KeyCodes.KEY_A.value() + "", KeyCodes.KEY_B.value() + "", KeyCodes.KEY_UP1.value() + "", KeyCodes.KEY_RIGHT1.value() + "", KeyCodes.KEY_LEFT1.value() + "", KeyCodes.KEY_DOWN1.value() + "", KeyCodes.KEY_LEFT.value() + "", KeyCodes.KEY_UP.value() + "", KeyCodes.KEY_RIGHT.value() + "", KeyCodes.KEY_DOWN.value() + ""};
    public static final String[] wscDefKeysValue  = {"KEY_SELECT", "KEY_START", "KEY_A", "KEY_B", "KEY_L1", "KEY_R1", "KEY_L2", "KEY_R2", "KEY_DPAD_LEFT", "KEY_DPAD_UP", "KEY_DPAD_RIGHT", "KEY_DPAD_DOWN"};

    //=====gbc key-value section=====ok
    public static final String   gbcKeyMapSection = "GBC_KEY_MAP";
    public static final String[] gbcKeysKey       = {KeyCodes.KEY_SELECT.value() + "", KeyCodes.KEY_START.value() + "", KeyCodes.KEY_D.value() + "", KeyCodes.KEY_C.value() + "", KeyCodes.KEY_B.value() + "", KeyCodes.KEY_A.value() + "", (KeyCodes.KEY_A.value() | KeyCodes.KEY_B.value()) + ""};
    public static final String[] gbcDefKeysValue  = {"KEY_SELECT", "KEY_START", "KEY_A", "KEY_B", "KEY_X", "KEY_Y", "KEY_R1"};

    //=====dc key-value section=====
    public static final String   dcKeyMapSection = "DC_KEY_MAP";
    public static final String[] dcKeysKey       = {KeyCodes.KEY_START.value() + "", KeyCodes.KEY_A.value() + "", KeyCodes.KEY_B.value() + "", KeyCodes.KEY_C.value() + "", KeyCodes.KEY_D.value() + "", KeyCodes.KEY_L1.value() + "", KeyCodes.KEY_R1.value() + ""};
    public static final String[] dcDefKeysValue  = {"KEY_START", "KEY_A", "KEY_B", "KEY_X", "KEY_Y", "KEY_L1", "KEY_R1"};

    //=====n64 key-value section=====
    public static final String   n64KeyMapSection = "N64_KEY_MAP";
    public static final String[] n64KeysKey       = {KeyCodes.KEY_START.value() + "", KeyCodes.KEY_A.value() + "", KeyCodes.KEY_B.value() + "", KeyCodes.KEY_C.value() + "", KeyCodes.KEY_D.value() + "", KeyCodes.KEY_L1.value() + "", KeyCodes.KEY_R1.value() + "", KeyCodes.KEY_L2.value() + "", KeyCodes.KEY_R2.value() + "", KeyCodes.KEY_LEFT1.value() + ""};
    public static final String[] n64DefKeysValue  = {"KEY_START", "KEY_A", "KEY_B", "KEY_X", "KEY_Y", "KEY_L1", "KEY_R1", "KEY_L2", "KEY_R2", "KEY_BUTTON_THUMBL"};

    //=====emu key-value section=====
    public static final String   emuKeyMapSection = "EMU_KEY_MAP";
    public static final String[] emuKeysKey       = {"hideVirtual", fbaKeyMapSection, fcKeyMapSection, sfcKeyMapSection, gbaKeyMapSection, pspKeyMapSection, mdKeyMapSection, psKeyMapSection, wscKeyMapSection, gbcKeyMapSection, dcKeyMapSection, n64KeyMapSection};
    public static final String[] emuDefKeysValue  = {"true", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1"};//0-off keymap, 1-on keymap
    public static final String[] sections         = {keySection, emuKeyMapSection, fbaKeyMapSection, sfcKeyMapSection, fcKeyMapSection, gbaKeyMapSection, pspKeyMapSection, mdKeyMapSection, psKeyMapSection, wscKeyMapSection, gbcKeyMapSection, dcKeyMapSection, n64KeyMapSection};

    public enum EmuMap {
        FBA(fbaKeyMapSection, fbaKeysKey, fbaDefKeysValue),
        FC(fcKeyMapSection, fcKeysKey, fcDefKeysValue),
        SFC(sfcKeyMapSection, sfcKeysKey, sfcDefKeysValue),
        PSP(pspKeyMapSection, pspKeysKey, pspDefKeysValue),
        GBA(gbaKeyMapSection, gbaKeysKey, gbaDefKeysValue),
        MD(mdKeyMapSection, mdKeysKey, mdDefKeysValue),
        PS(psKeyMapSection, psKeysKey, psDefKeysValue),
        WSC(wscKeyMapSection, wscKeysKey, wscDefKeysValue),
        GBC(gbcKeyMapSection, gbcKeysKey, gbcDefKeysValue),
        DC(dcKeyMapSection, dcKeysKey, dcDefKeysValue),
        N64(n64KeyMapSection, n64KeysKey, n64DefKeysValue)
        ;

        public String                  section;
        public String[]                keysKey;
        public HashMap<String, String> map;

        EmuMap(String section, String[] keysKey, String[] defKeysValue) {
            this.section = section;
            this.keysKey = keysKey;

            if (keysKey == null) return;

            boolean defValueNull = false;
            if (defKeysValue == null || defKeysValue.length != keysKey.length) defValueNull = true;

            map = new HashMap();
            for (int i = 0; i < keysKey.length; i++) {
                if (defValueNull)
                    map.put(keysKey[i], "");
                else
                    map.put(keysKey[i], defKeysValue[i]);
            }

        }

        public static EmuMap getEmuMap(String section) {
            EmuMap ret = null;
            if (section == null || section.trim().equals("")) return ret;
            for (EmuMap emuMap :
                    EmuMap.values()) {
                if (emuMap.section.equals(section)) {
                    ret = emuMap;
                    break;
                }
            }
            return ret;
        }

        public String defValue(String key) {
            if (map != null) return "";
            return map.get(key);
        }

    }
}
